import json
from PyQt5.QtCore import QThread
from enum import Enum

class jointPos:

    posDict={}
    maxPosNo=100

    def __init__(self):
        pass

    def addPos(self,pos,name=None):

        if name!=None:
            if not name in self.posDict.keys():
                self.posDict[name]=pos
            else:
                for i in range(self.maxPosNo):
                    tempName=name+str(i)
                    if not tempName in self.posDict.keys():
                        self.posDict[tempName]=pos
                        break
        else:
            for i in range(self.maxPosNo):
                tempName="pos"+str(i)
                if not tempName in self.posDict.keys():
                    self.posDict[tempName]=pos
                    break               

    def savePos(self,name=None):
        if name==None:
            filename=self.__class__.__name__+".json"
            with open(filename, "w") as outfile:
                json.dump(self.posDict, outfile)
        #print(self.__class__.__name__)


    def loadPos(self,name=None):
         if name==None:
            filename=self.__class__.__name__+".json"       
            with open(filename) as infile:
                try:
                    self.posDict = json.load(infile)
                    #print(self.posDict)
                except:
                    pass
class block:
    name=""
    type=""
    param=""
    def __init__(self,_name,_type,_param=None):
        self.type=_type
        self.name=_name
        if _param!= None:
            self.param=_param

class blockType(Enum):
    PTP=1
    WaitP=2
    Delay=3
    ITP=4

class CreateProgram():
    progList=[]
    maxBlockNo=100

    def __init__(self,_progName=None):
        #super(runProgram, self).__init__() 
        #self.name=hex(id(self))
        self.name=_progName
        #print(self.name)

    def addBlock(self,_name,_type,_param=None):
        self.progList.append(block(_name,_type,_param))

    def run(self):
        pass


    def loadProg(self,name=None):
        if name==None:
            if self.name!=None:
                filename=self.name+".json"
            else:
                filename=self.__class__.__name__+".json"
        else:
            filename=name+".json"
        with open(filename) as infile:
            try:
                tempList = json.load(infile)
                print(tempList)
            except:
                pass

        if len(tempList)>0:
            self.progList=[]
            for i in tempList:
                print(i)
                self.addBlock(i['name'],blockType(i['type']),i['param']) 
                #print(i.name,blockType(i.type),i.param)
            

        

    def saveProg(self,name=None):
        tempProgList=[]
        for i in self.progList:
            tempProgList.append({'name':i.name,'type':i.type.value,'param':i.param})
        #print(tempProgList)
        if name==None:
            if self.name!=None:
                filename=self.name+".json"
            else:
                filename=self.__class__.__name__+".json"
        else:
            filename=name+".json"
        with open(filename, "w") as outfile:
            json.dump(tempProgList, outfile)


class runProgram(QThread):

    def __init__(self,_progList):
        super(runProgram, self).__init__()
        self.progList=_progList   

    